﻿#include "register.h"
#include "ui_register.h"
#include "login.h"

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);//将窗口标题去掉
}

Register::~Register()
{
    delete ui;
}

void Register::mousePressEvent(QMouseEvent *event)
{
    isPressedWidget = true; // 当前鼠标按下的是QWidget而非界面上布局的其它控件
    last = event->globalPos();
}

void Register::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        move(x()+dx, y()+dy);
    }
}

void Register::mouseReleaseEvent(QMouseEvent *event)
{
    int dx = event->globalX() - last.x();
    int dy = event->globalY() - last.y();
    move(x()+dx, y()+dy);
    isPressedWidget = false; // 鼠标松开时，置为false
}

void Register::on_toolButton_clicked()
{
    close();
}

void Register::on_pushButton_clicked()//注册
{
    QString nickname = ui->nickname->text();
    QString pwd = ui->pwd->text();
    QString ans = ui->lineEdit->text();
    userInfo u;
    memset(&u,0,sizeof (u));
    memcpy(u.name,nickname.toUtf8(),nickname.toUtf8().length());
    memcpy(u.pwd,pwd.toUtf8(),pwd.toUtf8().length());
    PDU* pdu = makePDU(0);
    pdu->msgType = RES;
    Widget::getInstance().getTcpSocket().write((char*)pdu,pdu->maxSize);
    Widget::getInstance().getTcpSocket().write((char*)&u,sizeof (u));
    Widget::getInstance().getTcpSocket().write(ans.toUtf8(),ans.toUtf8().length()+1);
    delete pdu;
    pdu = nullptr;
}
